/**
 * @file    pcs_rs485_mng.c
 * @brief 
 * @author  Liuwei
 * @version 1.0.1
 * @date    2023-07-10
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-07-10 <td>1.0.1     <td>name     <td>首次创建
 * </table>
 */

/* includes ------------------------------------------------------------------*/
#include "pcs_rs485_mng.h"
#include "param_mng.h"
#include "voltronic_rs485.h"
#include "pylon_rs485.h"
#include "growatt_rs485.h"
#include "deye_rs485.h"
#include "invt_rs485.h"
#include "srne_rs485.h"
#include "iypower_rs485.h"
#include "smk_rs485.h"
#include "cfg_prj.h"
#include "hnjd_rs485.h"
#include "modbus_app.h"
#include "pace_modbus.h"
#include "huawei_rs485.h"
#include "pc_pace.h"
#include "pylon_rs485_v35.h"
#include "hnsh_rs485.h"
#include "vmii_rs485.h"
#include "deye_1363_rs485.h"
/* macro/define --------------------------------------------------------------*/

/* typedef -------------------------------------------------------------------*/

/* local functions -----------------------------------------------------------*/
//static uint8_t pcs_rs485_modbus_deal(uint8_t *rx_buf , uint16_t rx_len ,uint8_t *tx_buf , uint16_t *tx_len);
/* static variables ----------------------------------------------------------*/

/* global variables ----------------------------------------------------------*/

/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
/**
 * @brief  PCS RS485通信管理
 * @param  can_id
 * @param  cycle
 */
uint8_t pcs_rs485_comm_process(uint8_t *rx_buf , uint16_t rx_len ,uint8_t *tx_buf , uint16_t *tx_len)
{
    uint8_t pcs_type = 0;
	uint8_t res = FALSE;
    pcs_type = param_get_data(PAR_ID_PCS_RS485_TYPE);
	
    switch(pcs_type)
    {
        case PCS_RS485_PYLON:    /* (00) 派能协议 */
			res = pylon_rs485_v35_analysis(rx_buf , rx_len , tx_buf , tx_len);
			if(FALSE == res)
			{
				res = voltronoic_rs485_analysis(rx_buf ,rx_len ,tx_buf ,tx_len);
			}
            break;
        case PCS_RS485_GROWATT:  /* (01) 古瑞瓦特协议 */
            res = growatt_rs485_analysis(rx_buf , rx_len , tx_buf , tx_len);
			if(FALSE == res)
			{
				res = pylon_rs485_analysis(rx_buf , rx_len , tx_buf , tx_len);
			}
            break;
        case PCS_RS485_VOLTRONIC:   /* (02) 日月元 */
            res = voltronoic_rs485_analysis(rx_buf , rx_len , tx_buf , tx_len);
			if(FALSE == res)
			{
				res = pylon_rs485_analysis(rx_buf , rx_len , tx_buf , tx_len);
			}
            break;
        case PCS_RS485_LXP:    /* (03) 鹏程协议 和 派能一样*/
			res = pylon_rs485_analysis(rx_buf , rx_len , tx_buf , tx_len);
			if(FALSE == res)
			{
				res = voltronoic_rs485_analysis(rx_buf ,rx_len ,tx_buf ,tx_len);
			}
            break;
        case PCS_RS485_DEYE:  /* (04) 德业协议 */
			res = deye_rs485_analysis(rx_buf , rx_len , tx_buf , tx_len);
		    if(FALSE == res)
			{
				res = deye_1363_rs485_v35_analysis(rx_buf ,rx_len ,tx_buf ,tx_len);
			}
            break;
        case PCS_RS485_INVT: /* (05) 英威腾 - 和日月元一样 */
            res = voltronoic_rs485_analysis(rx_buf , rx_len , tx_buf , tx_len);
			if(FALSE == res)
			{
				res = pylon_rs485_analysis(rx_buf ,rx_len ,tx_buf ,tx_len);
			}
            break;
		case PCS_RS485_SRNE:  /* (06) 硕日 */
            res = srne_rs485_analysis(rx_buf , rx_len , tx_buf , tx_len);
			if(FALSE == res)
			{
				res = pylon_rs485_analysis(rx_buf , rx_len , tx_buf , tx_len);
			}
			break;
		case PCS_RS485_IY:     /* (07)爱阳协议 */
			res = iypower_rs485_analysis(rx_buf , rx_len , tx_buf , tx_len);
			if(FALSE == res)
			{
				res = pylon_rs485_analysis(rx_buf , rx_len , tx_buf , tx_len);
			}
			break;
		case PCS_RS485_SMK:   /* (08) SMK*/
			res = smk_rs485_analysis(rx_buf , rx_len , tx_buf , tx_len);
			if(FALSE == res)
			{
				res = voltronoic_rs485_analysis(rx_buf ,rx_len ,tx_buf ,tx_len);
			}
			break;
		case PCS_RS485_PACE:   /* (09) 沛城*/
			res = pace_modbus_analysis(rx_buf , rx_len , tx_buf , tx_len);
			if(FALSE == res)
			{
				res = pc_pace_analysis(rx_buf , rx_len , tx_buf , tx_len);
			}
			break;
		case PCS_RS485_HNJD:   /* (10) 汇能精电 */
			res = hnjd_rs485_analysis(rx_buf , rx_len , tx_buf , tx_len);
			if(FALSE == res)
			{
				res = voltronoic_rs485_analysis(rx_buf ,rx_len ,tx_buf ,tx_len);
			}
			if(FALSE == res)
			{
				res = pylon_rs485_analysis(rx_buf , rx_len , tx_buf , tx_len);
			}
			break;
		case PCS_RS485_SAKO:   /* (11) 扩展2*/
			res = pylon_rs485_analysis(rx_buf , rx_len , tx_buf , tx_len);
			if(FALSE == res)
			{
				res = voltronoic_rs485_analysis(rx_buf ,rx_len ,tx_buf ,tx_len);
			}
			break;
		case PCS_RS485_VMII:   /* (12) 扩展1*/
			res = vmii_rs485_analysis(rx_buf , rx_len , tx_buf , tx_len);
			if(FALSE == res)
			{
				res = pylon_rs485_analysis(rx_buf ,rx_len ,tx_buf ,tx_len);
			}
			break;
		case PCS_RS485_GOGHAMP:   /* (13) 扩展2*/
			res = hnsh_modbus_analysis(rx_buf , rx_len , tx_buf , tx_len);
			if(FALSE == res)
			{
				res = pylon_rs485_analysis(rx_buf ,rx_len ,tx_buf ,tx_len);
			}
			break;
		case PCS_RS485_EXP8:   /* (14) 扩展1*/
			res = pylon_rs485_analysis(rx_buf , rx_len , tx_buf , tx_len);
			if(FALSE == res)
			{
				res = huawei_rs485_analysis(rx_buf ,rx_len ,tx_buf ,tx_len);
			}
			break;
		case PCS_RS485_EXP9:   /* (15) 扩展2*/
			res = pylon_rs485_analysis(rx_buf , rx_len , tx_buf , tx_len);
			if(FALSE == res)
			{
				res = voltronoic_rs485_analysis(rx_buf ,rx_len ,tx_buf ,tx_len);
			}
			break;
        default:  // 默认派能 
			res = pylon_rs485_analysis(rx_buf , rx_len , tx_buf , tx_len);
			if(FALSE == res)
			{
				res = voltronoic_rs485_analysis(rx_buf ,rx_len ,tx_buf ,tx_len);
			}
            break;
    }
	
	if(FALSE == res)
	{
		res = hnjd_rs485_analysis(rx_buf , rx_len , tx_buf , tx_len);
	}

	return res ;
}

/**
 * @brief  PCS 读取RS485逆变器波特率
 * @param  void
 * @param  void
 */
uint32_t pcs_rs485_get_com_rate(void)
{
	return 9600;
}

///**
// * @brief  PCS-modbus协议自动识别
// * @param  只识别地址为 0x01
// * @param  void
// */
//static uint8_t pcs_rs485_modbus_deal(uint8_t *rx_buf , uint16_t rx_len ,uint8_t *tx_buf , uint16_t *tx_len)
//{
//	uint8_t res = 0;
//	md_data_type recv_md_data = {0};
//	if(TRUE == modbus_rtu_analysis(rx_buf , rx_len , &recv_md_data))
//	{
//		if(0x01 == recv_md_data.addr)
//		{
//			/* 0x03 命令码 */
//			if(0x03 == recv_md_data.cmd_code)
//			{	
//				/* 古瑞瓦特 */ 
//				if((recv_md_data.reg_addr == 0x13) && (recv_md_data.data_len == 0x10))
//				{
//					res = growatt_rs485_analysis(rx_buf ,rx_len ,tx_buf ,tx_len);
//				}
////				/* 硕日 */
////				else if(((recv_md_data.reg_addr == 0x00) && (recv_md_data.data_len == 0x0C))
////						||((recv_md_data.reg_addr == 0x25) && (recv_md_data.data_len == 0x03)))
////				{
////					res = srne_rs485_analysis(rx_buf ,rx_len ,tx_buf ,tx_len);
////				}
//				/* 日月元 */
//				else if(((recv_md_data.reg_addr == 0x00) && (recv_md_data.data_len == 0x0C))
//						||((recv_md_data.reg_addr == 0x25) && (recv_md_data.data_len == 0x03)))
//				{
//					res = voltronoic_rs485_analysis(rx_buf ,rx_len ,tx_buf ,tx_len);
//				}
//				else
//				{
//					res = FALSE;
//				}
//			}
//			else 
//				res = FALSE;
//		}
//		else
//		{
//			res = FALSE;
//		}
//	}
//	else
//	{
//		res = FALSE;
//	}
//	
//	return res;
//}
/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */

/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/
